package cn.tedu.controller;

import cn.tedu.pojo.Role;
import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户数据的控制层
 */
@Controller
@RequestMapping
public class UserController {
    // 控制层依赖于业务层，因此导入
    @Autowired
    private UserService userService;

    // 路径：/dologin.html 方法是：POST
    @RequestMapping(value = "/dologin.html", method = RequestMethod.POST)
    public String login(
            String userCode, String userPassword,
            HttpSession session,
            Model model ){
        User user = userService.login(userCode,userPassword);
        if (user != null){
            // 把当前用户数据存入session, 为了以后进行身份校验【拦截器】
            session.setAttribute("userSession", user);
            return "index";
        }
        // 程序走到这里，表示登录失败
        model.addAttribute("msg","用户名或者密码错误，请重新登录！");
        return "login";
    }

    // 路径: sys/user/List.html 方式
    @RequestMapping("/sys/user/list.html")
    public String getUserList(String queryUserName,Integer queryUserRole,Integer pageIndex,Model model){
        System.out.println(queryUserName);
        System.out.println(queryUserRole);
        System.out.println(pageIndex);
        System.out.println("=============================");
        Integer pageSize = 3;
        if (pageIndex == null){
            pageIndex = 1;
        }

        PageInfo<User> users = userService.findAll(queryUserName,queryUserRole,pageIndex,pageSize);
        List<Role> roles = userService.getRoleList();
        model.addAttribute("pi",users);
        model.addAttribute("roleList",roles);
        model.addAttribute("queryusername",queryUserName);
        model.addAttribute("querUserRole",queryUserRole);
        return "userlist";
    }

}

